package com.wms.service.InStore;

import com.wms.common.BusinessException;
import com.wms.common.PageInfo;
import com.wms.common.Utils;
import com.wms.dao.InStoreMapper;
import com.wms.dao.StorehouseMapper;
import com.wms.domain.InStoreDetailDomain;
import com.wms.domain.InStoreIdxDomain;
import com.wms.domain.StorehouseDomain;
import com.wms.dto.InStoreDetailDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class InStoreImpl implements InStoreService{

    private static final Logger log = LoggerFactory.getLogger(InStoreImpl.class);
    @Autowired
    private InStoreMapper inStoreMapper;
    @Autowired
    private StorehouseMapper storehouseMapper;
    @Transactional
    @Override
    public Object addNotice(InStoreIdxDomain inStoreIdxDomain) {
        log.info("add notice"+inStoreIdxDomain.toString());
        inStoreIdxDomain.setDate(Utils.getDate());
        if(inStoreIdxDomain.getId() != null){
            inStoreMapper.noticeUpdate(inStoreIdxDomain);
            inStoreMapper.deleteNoticeDetail(inStoreIdxDomain.getId());
        }else{
            inStoreIdxDomain.setIn_number(Utils.getTableNumber("in_store_idx","in_number"));
            inStoreMapper.addNotice(inStoreIdxDomain);
        }

        List<InStoreDetailDomain> list = inStoreIdxDomain.getDetail();
        for (InStoreDetailDomain detail :list){
            detail.setIn_store_id(inStoreIdxDomain.getId());
            inStoreMapper.addNoticeDetail(detail);
        }
        return null;
    }

    @Override
    public Object getNoticePage(InStoreIdxDomain inStoreIdxDomain) {
        log.info(inStoreIdxDomain.toString());
        PageInfo page = new PageInfo();
        List<HashMap<String,Object>> list = inStoreMapper.getNoticePage(inStoreIdxDomain);
        List<HashMap<String,Object>> result = new ArrayList<HashMap<String,Object>>();
        for (HashMap<String,Object> map : list){
            List<HashMap<String, Object>> detail= inStoreMapper.getInStoreDetail(((Integer) map.get("id")).longValue());
            if(detail != null){
                map.put("children",detail);
                map.put("row_key",map.get("id"));
            }
            result.add(map);
        }
        page.setData(result);
        page.setCount(inStoreIdxDomain.getBaseCount());
        page.setSum(inStoreIdxDomain.getBaseSum());
        return page;
    }

    @Override
    public Object getArrivedPage(InStoreDetailDto inStoreDetailDto) {
        log.info(inStoreDetailDto.toString());
        PageInfo page = new PageInfo();
        List<HashMap<String,Object>> list = inStoreMapper.getArrivedPage(inStoreDetailDto);
        page.setData(list);
        page.setCount(inStoreDetailDto.getBaseCount());
        page.setSum(inStoreDetailDto.getBaseSum());
        return page;
    }

    @Transactional
    @Override
    public Object noticeUpdate(InStoreIdxDomain inStoreIdxDomain) {
        inStoreMapper.noticeUpdate(inStoreIdxDomain);
        return null;
    }


    @Transactional
    @Override
    public Object updateDetail(InStoreDetailDomain inStoreDetailDomain) {
        inStoreMapper.updateDetail(inStoreDetailDomain);
        return null;
    }

    @Transactional
    @Override
    public Object inStoreFinish(Long id) {
        if(id == null){
            throw new BusinessException(202, "请选择商品");
        }

        InStoreDetailDomain detail = inStoreMapper.getInStoreDetailById(id);
        if(Utils.isBigDecimal(detail.getQty()) || detail.getStore_location_id() == null){
            throw new BusinessException(202,"当前货位数量不可以小于0");
        }
        StorehouseDomain sh = storehouseMapper.getStorehouseByG_idAndStoreL_id(detail.getG_id(),detail.getStore_location_id());
        InStoreIdxDomain idx = inStoreMapper.getInStoreIdxById(detail.getIn_store_id());
        if(idx.getC_id() == null){
            throw new BusinessException(202,"选择供应商");
        }
        StorehouseDomain storehouse = new StorehouseDomain();
        storehouse.setC_id(idx.getC_id());
        storehouse.setStore_location_id(detail.getStore_location_id());
        storehouse.setG_id(detail.getG_id());
        storehouse.setDate(Utils.getDate());
        if(sh == null){
            storehouse.setQty(detail.getQty());
            storehouseMapper.insertStorehouse(storehouse);
        }else{
            storehouse.setId(sh.getId());
            storehouse.setQty(sh.getQty().add(detail.getQty()));
            storehouseMapper.updateStorehouse(storehouse);
        }
        //完成
        InStoreDetailDomain isdd = new InStoreDetailDomain();
        isdd.setId(id);
        isdd.setIn_store_stat(6L);

        InStoreIdxDomain isid = new InStoreIdxDomain();
        isid.setId(detail.getIn_store_id());
        isid.setIn_store_finish(1L);
        isid.setDate(Utils.getDate());

        inStoreMapper.updateDetail(isdd);
        inStoreMapper.noticeUpdate(isid);


        return null;
    }
}
